package com.xingfly.web.model;

import org.springframework.http.HttpStatus;

/**
 * 返回格式模型
 * Created by SuperS on 25/12/2016.
 */
public class ResponseModel<T> {

    private int code;
    private String msg;
    private T data;


    public static <T> ResponseModel<T> ok(T data) {
        return new ResponseModel<T>(data).code(HttpStatus.OK.value()).message(HttpStatus.OK.name());
    }

    public static <T> ResponseModel<T> error(HttpStatus status) {
        return new ResponseModel<T>().code(status.value()).message(status.name());
    }

    public static <T> ResponseModel<T> error(HttpStatus status, String message) {
        return new ResponseModel<T>().code(status.value()).message(message);
    }

    public static <T> ResponseModel<T> create(T data,String message){
        return new ResponseModel<T>(data).code(HttpStatus.OK.value()).message(message);
    }

    public static <T> ResponseModel<T> create(T data, HttpStatus status) {
        return new ResponseModel<T>(data).code(status.value()).message(status.name());
    }

    public static <T> ResponseModel<T> create(T data, HttpStatus status, String msg) {
        return new ResponseModel<T>(data).code(status.value()).message(msg);
    }

    public ResponseModel<T> code(int code) {
        this.code = code;
        return this;
    }

    public ResponseModel<T> message(String message) {
        this.msg = message;
        return this;
    }

    ResponseModel() {
    }

    public ResponseModel(T data) {
        this.data = data;
    }

    public ResponseModel(int code, T data) {
        this(data);
        this.code = code;
    }

    public ResponseModel(int code, T data, String msg) {
        this(code, data);
        this.msg = msg;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
